package com.bos.controller;

import com.bos.data.model.*;
import com.bos.service.LogsService;
import com.bos.service.ProjectPropertyService;
import com.bos.service.ProjectService;
import com.bos.service.Project_itemSerice;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class Project_itemController {
    int id = 0;
    @Autowired
    @Qualifier("itemServiceImpl")
    Project_itemSerice itemService;
    @Autowired
    private ProjectService projectService;

    @Autowired
    private ProjectPropertyService projectPropertyService;

    @Autowired
    private LogsService logsService;

    //传项目id给添加字段的操作
    @RequestMapping("/sendPid")
    public String sendPind() {
        return "jsp/add";
    }


    //1.添加条件的操作，这个方法跟下面那个方法一起操作
    @RequestMapping(value = "/addZiDuan")
    public String addZiduan(Model model, @RequestParam("aname") String aname,
                            HttpServletRequest request, HttpSession session) throws Exception {
        //得到从页面传过来的字段名称名字和属性值插入到project_property中(pid为项目id)
        ProjectPropertyModel pro = new ProjectPropertyModel();
        pro.setProp(aname);//字段名称
        pro.setType("varchar(100)");//字段类型

        ProjectPropertyModel result = itemService.findPropertyByProp(aname);
        if (result != null) {
            model.addAttribute("errorMsg", "该字段已存在,添加失败");
        } else {
            int row = itemService.insertProperty(pro);
            //辅助一份到历史表中
            ProjectPropertyHistoryModel history=new ProjectPropertyHistoryModel();
            BeanUtils.copyProperties(pro,history);
//            int newId=itemService.addProProHistory(history);

            //获取project_property这个表新增这个属性的id
            ProjectPropertyModel property = itemService.findPropertyByProp(aname);
            //将项目id,属性id和属性值保存到project_item中
            List<BosProjectModel> plist = projectService.findProject();
            List<ProjectItemModel> itemList=new ArrayList<>();
            List<ProjectItemHistoryModel> itemHistoryList=new ArrayList<>();
            for (int i = 0; i < plist.size(); i++) {
                BosProjectModel p = plist.get(i);
                ProjectItemModel item = new ProjectItemModel();
                item.setProid(p.getId());//项目id
                item.setProitid(property.getId());//Project_property表中的属性id
                itemList.add(item);
                //复制一份到历史表中
                ProjectItemHistoryModel historyModel=new ProjectItemHistoryModel();
                BeanUtils.copyProperties(item,historyModel);
                itemHistoryList.add(historyModel);
            }
            itemService.insertItem(itemList);
            itemService.insertItemHistory(itemHistoryList);
            model.addAttribute("successMsg", "true");
        }
        //插入日志
        logsService.insertLogs(session, "添加了新字段 ["+aname+"]");
        return "jsp/add";

    }

    //目前存在问题：就是添加新字段能成功，就是不知道如何在显示新添加的字段
    @RequestMapping(value = "/showByNameAndValue")
    public String findByNameAndValue() {


        return "bosIndex";
    }

    @RequestMapping(value = "findAllColumn")
    public String findAllColumn(Model model) {
        //查出所有添加的新字段
        List<ProjectPropertyModel> list = projectPropertyService.getAllProperty();
        model.addAttribute("list", list);
        return "jsp/delColumn";
    }

    @RequestMapping(value = "delColumn/{id}/{name}")
    public String delColumn(Model model, @PathVariable("id") int id,@PathVariable("name") String name, HttpSession session) {
        int count = itemService.delByProitid(id);
        int count1 = projectPropertyService.delById(id);
        //历史表的数据也删除
        itemService.delHistoryByProitid(id);
        projectPropertyService.delHistoryById(id);
        model.addAttribute("count", count);
        model.addAttribute("count1", count1);
        //插入日志
        logsService.insertLogs(session, "删除了字段 ["+name+"]");
        return "redirect:../../findAllColumn";
    }
}
